<template>
  <a-card :bordered="false" class="card-area">
    <div class="table-page-search-wrapper">
      <a-form layout="vertical">
        <a-row :gutter="48">
          <a-col :sm="{span:24}" :lg="{span:12}" :xl="{span:6}">
            <a-form-item label="科目码">
              <a-input placeholder="请输入..." v-model="queryParam.kmbm" />
            </a-form-item>
          </a-col>
          <a-col :sm="{span:24}" :lg="{span:12}" :xl="{span:6}">
            <a-form-item label="科目名称">
              <a-input placeholder="请输入..." v-model="queryParam.kmmc" />
            </a-form-item>
          </a-col>
          <template v-if="advanced">
            <a-col :sm="{span:12}" :lg="{span:12}" :xl="{span:6}">
              <a-form-item label="招生年度">
                <a-select showSearch optionFilterProp="children" v-model="queryParam.zsxn" @change="xnChange">
                  <a-select-option v-for="i in TYPE0126" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :sm="{span:24}" :lg="{span:12}" :xl="{span:6}">
              <a-form-item label="院系">
                <a-select :disabled="selectMenu.yx.length === 1" :allowClear="true" showSearch optionFilterProp="children" v-model="queryParam.ssyxm" @change="yxChange">
                  <a-select-option v-for="i in selectMenu.yx" :key="i.id" :value="i.dwh">{{ i.dwh+"-"+ i.dwmc }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :sm="{span:12}" :lg="{span:12}" :xl="{span:6}">
              <a-form-item label="专业">
                <a-select :allowClear="true" showSearch optionFilterProp="children" v-model="queryParam.sszym" @change='zyChange'>
                  <a-select-option v-for="i in selectMenu.zy" :key="i.id" :value="i.zyh">{{ i.zyh+"-"+i.zymc }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :sm="{span:12}" :lg="{span:12}" :xl="{span:6}">
              <a-form-item label="研究方向">
                <a-select :allowClear="true" showSearch optionFilterProp="children" v-model="queryParam.yjfxm">
                  <a-select-option v-for="i in selectMenu.yjfx" :key="i.id" :value="i.yjfxm">{{ i.yjfxm.substring(i.yjfxm.length-2) +"-"+ i.yjfxmc }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
          </template>
          <a-col :sm="{span:24}" :xl="{span:12}">
             <span class="table-page-search-submitButtons" >
            <a-button type="primary" @click="addBypVisibile = true">添加(按专业)</a-button>
            <a-button type="primary" @click="addBydVisibile = true">添加(按方向)</a-button>
            <a-button type="primary" @click="loadData">查询</a-button>
            <a-button type="primary" @click="handleDelete">删除</a-button>
            <a-button type="primary" @click="exportExcel">导出</a-button>
            <a @click="advanced = !advanced" style="margin-left: 8px">
              {{ advanced ? '收起' : '展开' }}
              <a-icon :type="advanced ? 'up' : 'down'" />
            </a>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>

    <a-table ref="table" size="default" rowKey="key" :columns="columns" :loading="loading"
        :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
       :dataSource="data" :pagination="pagination" @change="handleTableChange">
      <template slot="action" slot-scope="text,record">
        <a @click="handleEdit(record.ID,record.YJFXM)">修改</a>
        <a @click="handleDeleteSingle(record.ID)">删除</a>
      </template>

      <span slot="serial" slot-scope="text, record, index">
        {{ index + 1 }}
      </span>
    </a-table>
    <!-- 新增 -->
    <test-add-p ref="testAddByP" @close="handleAddClose" @success="handleAddSuccess" :addVisibile="addBypVisibile"></test-add-p>
    <!-- 新增 -->
    <test-add-d ref="testAddByD" @close="handleAddClose" @success="handleAddSuccess" :addVisibile="addBydVisibile"></test-add-d>
    <!-- 修改 -->
    <test-edit-p ref="testEditByP" :info="testEditByPData" @close="handleEditClose" @success="handleEditSuccess"
      :editVisibile="editBypVisibile"></test-edit-p>
     <!-- 修改 -->
    <test-edit-d ref="testEditByD" :info="testEditByDData" @close="handleEditClose" @success="handleEditSuccess"
      :editVisibile="editBydVisibile"></test-edit-d>
  </a-card>
</template>

<script>
  // import moment from 'moment'
  import TestAddP from './testAddByP'
  import TestAddD from './testAddByD'
  import TestEditP from './testEditByP'
  import TestEditD from './testEditByD'
  import {
    STable
  } from '~'
  import {
    mapState
  } from 'vuex'

  const selectArr = ['TYPE0126', 'TYPE0075', 'TYPE0125', 'TYPE0072']

  export default {
    components: {
      STable,
      TestAddP,
      TestAddD,
      TestEditP,
      TestEditD
    },
    data() {
      return {
        advanced: true,
        loading: false,
        data: [],
        // 查询参数
        queryParam: {
          yjfxm: '',
          kmbm: '',
          kmmc: '',
          ssyxm: '',
          sszym: '',
          zsxn: ''
        },
        postParams: {},
        // 表头
        columns: [{
            title: '序号',
            scopedSlots: {
              customRender: 'serial'
            }
          },
          {
            title: '院系',
            dataIndex: 'SSYXMC'
          },
          {
            title: '专业',
            dataIndex: 'SSZYMC'
          },
          {
            title: '科目类型',
            dataIndex: 'KMLXM'
          },
          {
            title: '科目码',
            dataIndex: 'KMBM'
          },
          {
            title: '科目名称',
            dataIndex: 'KMMC'
          },
          {
            title: '招生年度',
            dataIndex: 'ZSXN'
          },
          {
            title: '研究方向',
            dataIndex: 'yjfxmc'
          },
          {
            title: '导师',
            dataIndex: 'dsxxmName'
          },
          {
            title: '操作',
            dataIndex: 'cz',
            fixed: 'right',
            width: 120,
            scopedSlots: {
              customRender: 'action'
            }
          }
        ],
        selectedRowKeys:[],
        selectedRows:[],
        options: {
          alert: { show: true, clear: () => { this.selectedRowKeys = [] } },
          rowSelection: {
            selectedRowKeys: this.selectedRowKeys,
             onChange: (selectedRowKeys, selectedRows) => {
              console.log(`selectedRowKeys: ${selectedRowKeys}`, 'selectedRows: ', selectedRows)
              this.selectedRowKeys = selectedRowKeys
              this.selectedRows = selectedRows
            },
            onSelect: (record, selected, selectedRows) => {
              console.log(record, selected, selectedRows)
            },
            onSelectAll: (selected, selectedRows, changeRows) => {
              console.log(selected, selectedRows, changeRows)
            },
          }
        },
        addBypVisibile: false,
        addBydVisibile: false,
        editBypVisibile: false,
        editBydVisibile: false,
        selectMenu: {
          yx: [], // 院系
          yjfx: [],
          zy: []
        },
        testEditByPData: {},
        testEditByDData: {},
        pagination: {
          current: 1
        }
      }
    },
    computed: {
      ...mapState({
        systemInfo: state => state.app.systemInfo,
        TYPE0126: state => state.app['TYPE0126'] // 年级
      })
    },
    created() {
      this.initSelectMenu()
      selectArr.forEach(v => {
        this.$store.dispatch('app/setDictionary', v)
      })
    },
    methods: {
      initSelectMenu() {
        let allRequest = [
          this.$api.base.collegeAll()
        ]
        Promise.all(allRequest).then(res => {
          this.selectMenu = {
            yx: res[0].data
          }
          this.queryParam.ssyxm = res[0].data[0].dwh
          this.getProssion(this.queryParam.ssyxm)
           // 获取当前学年
          this.$api.base.getSchoolYear().then(res=>{
            this.queryParam.zsxn = res.data.masterAdmissionYear
             this.loadData({
              pageSize: 10,
              pageNum: 1
            })
          })
        })
      },
        // 加载数据
      loadData(parameter = {}) {
        if(!parameter.pageNum){
          this.pagination.current = 1
          parameter.pageNum = 1
        }
        let params = Object.assign(this.queryParam, parameter)
        if(!this.queryParam.zsxn){
          this.$message.warn("请选择招生年度")
          return ;
        }
        this.loading = true;
        this.$api.recruitstat.entryResultManageList(params).then(res => {
          // console.log(res)
           const pagination = { ...this.pagination
          };
          pagination.total = 0
          if (res.data && res.data.total) {
            pagination.total = res.data.total;
          }
          this.data = []
          this.pagination = pagination;
          if (res.data && res.data.rows.length) {
            this.data = res.data.rows;
          }
          this.loading = false;
        })
      },
      // 分页
      handleTableChange(pagination, filters, sorter) {
        console.log(pagination);
        const pager = { ...this.pagination
        };
        pager.current = pagination.current;
        this.pagination = pager;
        this.loadData({
          pageSize: 10,
          pageNum: pagination.current
        });
      },
       // 多选
      onSelectChange (selectedRowKeys,selectedRows) {
        console.log('selectedRowKeys changed: ', selectedRowKeys);
        this.selectedRows = selectedRows
        this.selectedRowKeys = selectedRowKeys
      },
      // 选择院系
      yxChange(code) {
        this.$set(this.queryParam, 'sszym', '')
        this.$set(this.queryParam, 'yjfxm', '')
        this.$set(this.selectMenu, 'zy', [])
        this.$set(this.selectMenu, 'yjfx', [])
        if(code){
          this.getProssion(code)
        }
      },
      getProssion(code) {
        this.$api.base.majorAll({
          lsdwh: code
        }).then(res => {
          this.$set(this.selectMenu, 'zy', res.data)
        })
      },
      // 选择专业
      zyChange(code){
        this.$set(this.queryParam, 'yjfxm', '')
        this.$set(this.selectMenu, 'yjfx', [])
        if(code){
          this.$api.base.searchDirectory({
            ssyxm: this.queryParam.ssyxm,
            zsxn: this.queryParam.zsxn,
            sszym: code,
            xwccm: 2
          }).then(res => {
            this.$set(this.selectMenu, 'yjfx', res.data)
          })
        }
      },
      xnChange(code){
        this.$set(this.selectMenu, 'yjfx', [])
        this.$set(this.queryParam, 'yjfxm', '')
        if(this.queryParam.sszym && this.queryParam.sszym && code){
           this.$set(this.queryParam, 'yjfxm', '')
          let params={sszym: this.queryParam.sszym,ssyxm: this.queryParam.ssyxm,zsxn:code,xwccm: 2}
          this.$api.base.searchDirectory(params).then(res => {
            this.$set(this.selectMenu, 'yjfx', res.data)
          })
       }
      },
      // 修改
      handleEdit(key,fx) {
        console.log(fx)
        this.$api.recruitstat.entryResultManageId(key).then( res => {
            if(fx){
              this.editBydVisibile = true
              this.editBypVisibile = false
              this.testEditByDData = res.data
            }else{
              this.editBypVisibile = true
              this.editBydVisibile = false
              this.testEditByPData = res.data
            }
        })
      },
      // 关闭修改层
      handleEditClose() {
        this.editBypVisibile = false
        this.editBydVisibile = false
      },
      // 修改成功
      handleEditSuccess() {
        this.editBypVisibile = false
        this.editBydVisibile = false
        this.$message.success('编辑成功')
        this.loadData({
          pageSize:10,
          pageNum:this.pagination.current
        })
      },
      // 关闭添加弹层
      handleAddClose() {
        this.addBypVisibile = false
        this.addBydVisibile = false
      },
      // 添加成功
      handleAddSuccess() {
        this.addBypVisibile = false
        this.addBydVisibile = false
        this.$message.success('添加成功')
        this.loadData({
          pageSize:10,
          pageNum:1
        })
      },
      // 导出
      exportExcel() {
        console.log('导出')
        this.$export('admissions/ssrxkskmgl/export', this.queryParam)
      },
      // 删除
      handleDelete(key) {
        var deleteId = ''
        console.log('删除')
        if (!this.selectedRowKeys.length) {
          this.$message.warning('请选择需要删除的数据')
          return
        }
        if(this.selectedRows && this.selectedRows.length){
          deleteId = this.selectedRows.map(v => {
            return v.ID
          }).toString()
        }
        console.log(deleteId)
        var that = this;
        this.$confirm({
          title: '确定删除?',
          onOk() {
            console.log('OK');
            that.deleteData(deleteId)
          },
          onCancel() {
            console.log('Cancel');
          },
          class: 'test',
        });
      },
      handleDeleteSingle(key){
        var that = this;
        this.$confirm({
          title: '确定删除?',
          onOk() {
            console.log('OK');
            that.deleteData(key)
          },
          onCancel() {
            console.log('Cancel');
          },
          class: 'test',
        });
      },
      deleteData(code){
        this.$api.recruitstat.entryResultManageDelete(code).then( res => {
           this.loadData({
            pageSize:10,
            pageNum:1
          })
          this.selectedRowKeys = []
        })
      }
    }
  }
</script>
